package com.nongtai.controller;

import com.nongtai.constant.GoodsHotRankConstant;
import com.nongtai.exception.InvalidGoodsIdException;
import com.nongtai.exception.InvalidPaginationDataException;
import com.nongtai.pojo.vo.GoodsDetailsVo;
import com.nongtai.pojo.vo.GoodsVo;
import com.nongtai.result.Result;
import com.nongtai.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 商品相关
 *
 * @author yza
 * @version 1.0
 * @date 2024/6/17 17:28
 */
@RestController
@Slf4j
public class GoodsController {

    @Resource
    private GoodsService goodsService;

    /**
     * 根据商品id返回商品详情信息
     *
     * @param id 商品id
     * @return 商品详情信息
     */
    @GetMapping("/goods")
    public Result<GoodsDetailsVo> getGoodsDetails(Long id) {
        GoodsDetailsVo goodsDetails = goodsService.getGoodsDetailsById(id);
        return Result.success(goodsDetails);
    }

    /**
     * 获取热门商品
     * @param id 当前商品id
     * @param limit 限制返回几条数据
     * @param type 热销类型，1为24小时，2为周榜，3为总榜，默认为1
     * @return
     */
    @GetMapping("/goods/hot")
    public Result<List<GoodsVo>> getHotGoods(@Nullable Long id, Integer limit, String type) {
        if(id == null || id < 0 ){
            throw new InvalidGoodsIdException("商品id错误");
        }else if(limit == null || limit < 0 || limit > 200){
            throw new InvalidPaginationDataException("非法的分页数据");
        }

        List<GoodsVo> hotGoods = null;

        if(type.equals(GoodsHotRankConstant.HOT_ONE_DAY)){
            hotGoods = goodsService.getHotGoodsRankOneDay(id,limit);
        }else if(type.equals(GoodsHotRankConstant.HOT_ONE_WEEK)){
            hotGoods = goodsService.getHotGoodsRankOneWeek(id,limit);
        }else if (type.equals(GoodsHotRankConstant.HOT_ALL)){
            hotGoods = goodsService.getHotGoodsRankAll(limit);
        }else {
            throw new IllegalArgumentException("非法的热销类型参数");
        }

        return Result.success(hotGoods);
    }

}
